//
//  AppDelegate.swift
//  01MicroBlog
//
//  Created by Apple on 15/7/28.
//  Copyright © 2015年 heima. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 0.添加通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "chooseRootViewController:", name: WJRootViewControllerSwitchNotification, object: nil)
        
        // 1.创建一个window
         window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.rootViewController = defaultController()
        
        // 4.设置window可见
        window?.makeKeyAndVisible()
        
        // 设置导航条的按钮的线条颜色
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        // 设置tabBar的按钮的线条颜色
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        return true
    }
    
    // MARK: - 返回默认控制器
    private func defaultController() -> UIViewController {
        
        // 如果用户未登录，返回主控制器
        if !UserAccount.userLogon {
            return MainViewController()
        }
        // 判断系统版本，是否显示新特性
        return shouldShowNewProperties() ? NewFeatureViewController() : WelcomeViewController()
    }
    
    // 是否显示新版本
    private func shouldShowNewProperties() -> Bool{
        
        // 1. 获取程序当前的版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        // 2.取出沙盒中保存到程序版本
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        // 将当前版本保存到偏好设置中
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        // ios 7.0 之后，就不需要同步了，iso 6.0之前， 如果不同步不会第一时间写入沙盒
        NSUserDefaults.standardUserDefaults().synchronize()
        
        // 返回比较结果
        return currentVersion > sandboxVersion

    }
    
    // 监听通知，改变根控制器
     func chooseRootViewController(n:NSNotification) {
        
        print("切换控制器")
        let isMainVc = n.object as! Bool
        window?.rootViewController = isMainVc ? MainViewController() : WelcomeViewController()
        
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

